package com.ali.myshop.plus.business.controller;

import com.ali.myshop.plus.common.dto.ResponseResult;
import com.ali.myshop.plus.provider.api.UmsAdminService;
import com.ali.myshop.plus.provider.domain.UmsAdmin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author :ali
 * @date :2020/1/23 18:23
 * @description：注册
 * @modified By:
 * @version: 1.0.0
 */
//解决跨域
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping(value = "reg")
public class RegController {
    @Reference(version = "1.0.0")
    private UmsAdminService umsAdminService;

    /**
     * 用户注册
     * @param umsAdmin
     * @return {@link ResponseResult}
     */
    @PostMapping(value = "")
    public ResponseResult<UmsAdmin> reg(@RequestBody UmsAdmin umsAdmin){
        String message = validateReg(umsAdmin);
        //通过验证
        if (message == null) {
            int insert = umsAdminService.insert(umsAdmin);
            UmsAdmin umsAdminok = umsAdminService.get(umsAdmin.getUsername());
            //注册成功
            if (insert > 0){
                return new ResponseResult<UmsAdmin>(HttpStatus.OK.value(),"用户注册成功",umsAdminok);
            }
        }
        //注册失败
        return new ResponseResult<UmsAdmin>(HttpStatus.NOT_ACCEPTABLE.value(),message!=null?message:"用户注册失败");
    }

    /**
     * 注册信息验证
     * @param umsAdmin {@link UmsAdmin}
     * @return
     */
    private String validateReg(UmsAdmin umsAdmin){
        UmsAdmin admin = umsAdminService.get(umsAdmin.getUsername());
        if (admin != null) {
            return "用户名已经存在，请从新输入";
        }
        return null;
    }
}
